﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Web.Utility;
using App.Domain;
using App.Web.ViewModels;
using App.Web.ViewModels.UtilityModels;
using System.Data;

namespace App.Web.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class IndustryTypeController : BaseController
    {
        private AppEntities _Context = null;

        public IndustryTypeController()
        {
            _Context = new AppEntities();
        }
        //
        // GET: /IndustryType/
        //public ActionResult Index()
        //{
        //    return View();
        //}

        public ActionResult Edit(int id = 0, bool showSuccessMessage = false)
        {            
            IndustryType type = _Context.IndustryTypes.FirstOrDefault(P => P.ID == id);
            if (type == null)
                return View(new IndustryTypeViewModel());
            else
            {
                //Category category = _Context.Categories.FirstOrDefault(C => C.ID == 2);
                //AutoMapper.Mapper.CreateMap<IndustryType, IndustryTypeViewModel>();
                IndustryTypeViewModel vm = AutoMapper.Mapper.Map<IndustryType, IndustryTypeViewModel>(type);
                return View(vm);
            }
        }

        [HttpPost]
        public ActionResult Edit(IndustryTypeViewModel viewModel)
        {
            IndustryType type = null;            
            if (viewModel.ID == 0) //Insert Mode
            {
                type = new IndustryType();
                _Context.AddToIndustryTypes(type);
            }
            else
            {
                type = _Context.IndustryTypes.FirstOrDefault(P => P.ID == viewModel.ID);
            }
            ///Copy Property Values of ViewModel Object  to Domain DataModel Object
            type = AutoMapper.Mapper.Map<IndustryTypeViewModel, IndustryType>(viewModel, type);            
            //type.Name = viewModel.Name;
            _Context.SaveChanges();
            return RedirectToAction("List", "IndustryType");
        }        
        public ActionResult List()
        {
            UtilityDAO dao = new UtilityDAO();
            DataSet ds = dao.GetDataSet(DbObject.Sql.INDUSTRY_TYPE, null, false);
            return View(ds.Tables[0]);
        }
    }
}
